﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.Goods;
using Rabbit.API.DTOs;

namespace Rabbit.API.Application.Handler.Goods
{
    public class GoodsModelHandler:IRequestHandler<GoodsModelCommand,APIResult<GoodsDTO>>
    {
        private readonly IRepository<GoodsModel> goodsway;
        private readonly IMapper mapper;
        private readonly ILogger<GoodsGetHandler> log;

        public GoodsModelHandler(IRepository<GoodsModel> goodsway, IMapper mapper, ILogger<GoodsGetHandler> log)
        {
            this.goodsway = goodsway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 商品信息详情
        /// </summary>
        /// <param name="request">主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>商品信息</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<GoodsDTO>> Handle(GoodsModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<GoodsDTO> res = new APIResult<GoodsDTO>();
            try
            {
                var goods =await goodsway.GetValue(request.Id);
               
                var value = mapper.Map<GoodsDTO>(goods);
                if(value!=null)
                {
                    log.LogInformation($"获取商品详情成功");
                    res.Message = "获取商品详情成功";
                    res.Num = StateCode.OK;
                    res.Data = value;
                }
                else
                {
                    log.LogInformation($"获取商品详情失败");
                    res.Message = "获取商品详情失败";
                    res.Num = StateCode.Fail;
                }

                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
